package com.earnings.okhttputils.utils.ui.activity.user;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.Md5Utils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.Constant;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.activity.common.StatusActivity;

import java.util.HashMap;
import java.util.Map;

import static com.earnings.R.id.sumbit;


/*
编辑用户信息-设置支付密码
 */

public class UserEditPaypasswordSetNewActivity extends GodLeftHandBaseActivity implements View.OnClickListener {


    private Button button;

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_user_edit_paypsw_set;
    }

    @Override
    protected void init() {

        setTitle("支付密码设置");
        button=(Button)findViewById( sumbit);
        button.setText( "完成" );
        button.setOnClickListener( this );
        initCodeEdittext();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

    }


    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == sumbit) {
            if(bundleData.containsKey( "oldpwd" )){
                updatePayPassword();
            }
            else if(bundleData.containsKey( "code" )){
                forgetPassword();
            }else{
                createPayPassword();
            }
           // ToastUtils.showToast( getContext(), getPsw() );

        }
    }

public String getPsw(){
    String password="";
    for (int i = 1; i <=size ; i++) {
        int rid=getIdFromXML("code"+i);
        password+=((EditText)findViewById(rid)).getText().toString();
    }
    return password;
}

    /*
初始化验证码监听
 */
    final int size=6;
    public void initCodeEdittext(){
        final Map<Integer,EditText> codeMap=new HashMap<>();
        for (int i = 1; i <=size ; i++) {
            int rid=getIdFromXML("code"+i);
            final EditText code= (EditText) findViewById(rid);
            final int num=i;
            if(num==1){
                code.clearFocus();
                code.setFocusable(true);
                code.setFocusableInTouchMode(true);
                code.requestFocus();
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
            //code.setEnabled(false);
            codeMap.put(i,code);
            //监听输入
            code.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    code.clearFocus();
                    if(code.getText().toString().length()==1&&num!=size) {


                      //  code.setBackgroundResource(R.drawable.code_radius_false);
                        EditText nowEditText= codeMap.get(num + 1);

                        nowEditText.setText("");
                        nowEditText.setFocusable(true);
                        nowEditText.requestFocus();
                      //  nowEditText.setBackgroundResource(R.drawable.code_radius_true);
                    }else if(num==size&&code.getText().toString().length()!=0){

                        button.setBackgroundResource( R.drawable.btn_sumbit );
                        //checkode();
                    }
                }
            });
            //监听删除
            code.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {

                    button.setBackgroundResource( R.drawable.btn_sumbit );
                    if (keyCode == KeyEvent.KEYCODE_DEL
                            && event.getAction() == KeyEvent.ACTION_DOWN) {

                        if(num!=1) {

                            EditText nowEditText= codeMap.get(num -1);
                            code.clearFocus();
                           // code.setBackgroundResource(R.drawable.code_radius_false);
                            if(code.getText().toString().length()==0) nowEditText.setText("");
                            nowEditText.setFocusable(true);
                            nowEditText.requestFocus();
                          //  nowEditText.setBackgroundResource(R.drawable.code_radius_true);
                        }
                        code.setText("");
                        return true;
                    }else{
                        // code.setText(event.getNumber());
                    }
                    return false;
                }
            });

        }
    }


    public void createPayPassword() {
        Map map = new HttpMap(getContext());

        map.put( "paypwd",Md5Utils.getMd5Value(getPsw()));

        OkHttpUtils.post().url( HttpUrl.USER_PAY_PWD_SET_URL ).params( map ).build().execute( new HttpObjectCallback<String>(  getContext()) {
            @Override
            public void onSuccess(String object, String msg) {

                bundleData.putString( "title", "设置成功" );
                bundleData.putString( "info", "您已成功设置支付密码" );
                skipActivity( StatusActivity.class );
                setResult( Constant.USER_UPDATE );
                finish();
            }
            @Override
            public void onFail(int code, String msg) {
                ToastUtils.showToast( getContext(),msg );
            }


        } );

    }
    public void updatePayPassword() {
        Map map = new HttpMap(getContext());

        map.put( "old_paypwd", Md5Utils.getMd5Value(bundleData.getString( "oldpwd" )));
        map.put( "paypwd",Md5Utils.getMd5Value(getPsw()));

        OkHttpUtils.post().url( HttpUrl.USER_PAY_PWD_UPDATE_URL ).params( map ).build().execute( new HttpObjectCallback<String>(  getContext()) {
            @Override
            public void onSuccess(String object, String msg) {

                bundleData.putString( "title", "修改成功" );
                bundleData.putString( "info", "您已成功修改支付密码" );
                skipActivity( StatusActivity.class );
                setResult( Constant.USER_UPDATE );
                finish();
            }
            @Override
            public void onFail(int code, String msg) {
                ToastUtils.showToast( getContext(),msg );
            }


        } );

    }
    public void forgetPassword() {
        Map map = new HttpMap(getContext());

        map.put( "realname",bundleData.getString( "name" ));
        map.put( "idcard",bundleData.getString( "card" ));
        map.put( "code",bundleData.getString( "code" ));
        map.put( "paypwd",Md5Utils.getMd5Value(  getPsw()));

        OkHttpUtils.post().url( HttpUrl.USER_PAY_PWD_FORGET_URL ).params( map ).build().execute( new HttpObjectCallback<String>(  getContext()) {
            @Override
            public void onSuccess(String object, String msg) {

                bundleData.putString( "title", "重置成功" );
                bundleData.putString( "info", "您已成功重置支付密码" );
                skipActivity( StatusActivity.class );
                setResult( Constant.USER_UPDATE );
                finish();
            }
            @Override
            public void onFail(int code, String msg) {
                ToastUtils.showToast( getContext(),msg );
            }


        } );

    }
}
